package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.tradingservice.entity.ServiceProductRefund;
import com.yuyou.fn.tradingservice.mapper.ServiceProductRefundMapper;
import com.yuyou.fn.tradingservice.service.IServiceOrderItemService;
import com.yuyou.fn.tradingservice.service.IServiceOrderService;
import com.yuyou.fn.tradingservice.service.IServiceProductRefundService;
import com.yuyou.fn.tradingservice.vo.ProductRefundQueryVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * ServiceProductRefund 表数据服务层接口实现类
 */
@Service
public class ServiceProductRefundServiceImpl extends SuperServiceImpl<ServiceProductRefundMapper, ServiceProductRefund, Long> implements IServiceProductRefundService {

    @Resource
    private IServiceOrderService orderService;

    @Resource
    private IServiceOrderItemService itemService;

    @Override
    public Page<ServiceProductRefund> findSeviceRefundPage(ProductRefundQueryVo productRefundQueryVo, int pageNo, int pageSize) {
        Example<ServiceProductRefund> example = ServiceProductRefund.createExample();
        example.orderBy("updateTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria, productRefundQueryVo);
        return findPageByExample(example, pageNo, pageSize);
    }

    @Override
    public void saveOrUpdateProductRefundDetail(ProductRefundQueryVo productRefundQueryVo) {
        ServiceProductRefund serviceProductRefund = new ServiceProductRefund();
        BeanUtils.copyProperties(serviceProductRefund, productRefundQueryVo);
        if (productRefundQueryVo.getServiceProductRefundId() == null) {
            serviceProductRefund.setCreateTime(TimeUtils.now());
            serviceProductRefund.setUpdateTime(TimeUtils.now());
            serviceProductRefund.setStatus(0);
            insertSelective(serviceProductRefund);
            itemService.changeOrderItemStatus(serviceProductRefund.getOrderItemId(),4);
            //修改整张订单状态
            orderService.changOrderStatus(serviceProductRefund.getOrderId(),3);
        } else {
            serviceProductRefund.setUpdateTime(TimeUtils.now());
            updateSelectiveById(serviceProductRefund);
        }
    }

    @Override
    public ServiceProductRefund getOrderItemRefundDetail(Long orderItemId) {
        Example<ServiceProductRefund> example = ServiceProductRefund.createExample();
        Example.Criteria criteria = example.createCriteria();
        example.orderBy("createTime").desc();
        criteria.andEqualTo("orderItemId",orderItemId);
        return findOneByExample(example);
    }
}